package com.token.iot.bratterycharging.gateway.sender;

import java.util.Map;

import org.springframework.stereotype.Component;

import com.outstanding.framework.core.Logger;
import com.outstanding.framework.core.PendingException;
import com.outstanding.framework.core.TransactionContext;
import com.outstanding.framework.log.LoggerFactory;
import com.token.iot.bratterycharging.gateway.AbstractSender;
import com.token.iot.bratterycharging.gateway.ChannelHandlerContextManager;

import com.token.iot.bratterycharging.gateway.IDGeneration;
import com.token.iot.bratterycharging.gateway.Semaphore;
import com.token.iot.bratterycharging.gateway.SemaphoreManager;
import com.token.iot.bratterycharging.gateway.Sender;
import com.token.iot.bratterycharging.gateway.api.Constant;
import com.token.iot.bratterycharging.gateway.utils.ByteUtil;

import io.netty.channel.ChannelHandlerContext;

/**
 * 回应心跳
 * 
 * @author chenzhixuan
 *
 */
@Component("AXTSender")
public class AXTSender extends AbstractSender {

	private static ChannelHandlerContextManager manager = new ChannelHandlerContextManager();

	private static Logger loger = LoggerFactory.getLogger(AXTSender.class);

	@Override
	public Object send(String imei, Map<String, String> params) throws PendingException {
		//loger.info("Staring The  AXTSender ");
		// 查找对应的HandlerContext
		ChannelHandlerContext context = null;
		if (imei == null) {
			context = (ChannelHandlerContext) TransactionContext.getData(Constant.CHANNELCONTEXT);
			loger.info("get the context from the Transaction "+String.valueOf(context==null));
		} else {
			context = manager.getChannelHandlerContextByImei(imei);
		}

		String command = "AXT000000/P";
		byte[] c = ByteUtil.addFlag(command.getBytes());
		//loger.info("the send =" + new String(c));
		context.writeAndFlush(c);
		return null;

	}

}
